#!/bin/sh

test_description="test proc-receive version"

. ./lib/sharness.sh

REPO_ID=""

test_expect_success "[test-1] init repo and branch" '
	REPO_ID=$(find_repo | jq .id)
	if [ -n $REPO_ID ]; then
		delete_repo > delete_repo.resp 2>&1
		test_expect_code 0
	fi
	create_repo > create_repo.resp 2>&1
	REPO_ID=$(cat create_repo.resp) &&
	add_member $READ_LEVEL $another_user_external &&
	init_repo > init_repo.resp &&
	test_expect_code 0
'

test_expect_success "[test-2][non-protected][readonly] Normal push to branch fails" '
	echo $REPO_DIR
	echo ",world" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "[test-2][non-protected][readonly]" --author "dyrone <dyroneteng@gmail.com>" --date=now	>/dev/null 2>&1 &&
	test_must_fail git -C "$REPO_DIR" push	> test-2 2>&1 &&
	grep "error: failed to push some refs to" test-2
'

test_expect_success "[test-3][non-protected][read-write] Normal push to branch success" '
  add_member $WRITE_LEVEL $another_user_external &&
  echo "[test-3][non-protected][read-write]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-3][non-protected][read-write]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1  &&
  git -C "$REPO_DIR" push  > test-3 2>&1
  test_expect_code 0
'

test_expect_success "[test-4][protected][read-write] Normal push to branch fails" '
  protect_branch master > protect_branch.resp &&
  echo "[test-4][protected][read-write]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-4][protected][read-write]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  test_must_fail git -C "$REPO_DIR" push  > test-4 2>&1
  grep "error: failed to push some refs to" test-4
'

test_expect_success "[test-5][protected][read] Normal push to branch fails" '
  add_member $READ_LEVEL $another_user_external &&
  echo "[test-5][protected][read]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-4][protected][read]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  test_must_fail git -C "$REPO_DIR" push  > test-5 2>&1
  grep "error: failed to push some refs to" test-5
'

test_expect_success "[test-6][protected][read] agit flow create CR success" '
  branch_id=$(get_protected_branch | jq '.[0].id') &&
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  git -C "$REPO_DIR" reset --hard $root
  echo "[test-6][non-protected][read]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-6][protected][read]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1
  git -C "$REPO_DIR" push origin master:refs/for/master -o proc-receive-debug=1 > test-6 2>&1
  test_expect_code 0
'

test_expect_success "[test-7][protected][read-write] agit flow create CR success" '
  add_member $WRITE_LEVEL $another_user_external &&
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  git -C "$REPO_DIR" reset --hard $root
  echo "[test-7][protected][read-write]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-7][protected][read-write]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master:refs/for/master -o proc-receive-debug=1 > test-7 2>&1
  test_expect_code 0
'

test_expect_success "[test-8][non-protected][read] agit flow create CR success" '
  add_member $READ_LEVEL $another_user_external &&
  branch_id=$(get_protected_branch | jq '.[0].id') &&
  unprotect_branch $branch_id  &&
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  git -C "$REPO_DIR" reset --hard $root
  echo "[test-8][non-protected][read]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-8][non-protected][read]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1
  git -C "$REPO_DIR" push origin master:refs/for/master -o proc-receive-debug=1 > test-8 2>&1
  test_expect_code 0
'

test_expect_success "[test-9][non-protected][read-write] agit flow create CR success" '
  add_member $WRITE_LEVEL $another_user_external &&
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  git -C "$REPO_DIR" reset --hard $root &&
  echo "[test-9][non-protected][read-write]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-9][protected][read-write]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master:refs/for/master -o proc-receive-debug=1 > test-9 2>&1
  test_expect_code 0
'

test_expect_success "[test-10][protected][read] agit flow update CR update success, operator is CR author" '
  add_member $READ_LEVEL $another_user_external &&
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  mr_id=1
  protect_branch master &&
  cd "$REPO_DIR" &&
  git repo --single download $mr_id &&
  git checkout -b "tmp"`date '+%Y%m%d%H%M%S'` &&
  echo "[test-10][protected][read]" >> test.file &&
  git add test.file &&
  git commit -m "[test-10][protected][read]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  git repo upload --single --change $mr_id -o proc-receive-debug=1 > ../test-10 2>&1 &&
  cd "$SHARNESS_TRASH_DIRECTORY"
'

test_expect_success "[test-11][protected] agit flow update CR success, operator is CR reviewer" '
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  mr_id=1
  remove_all_reviewers $REPO_ID $mr_id &&
  add_reviewer $REPO_ID $mr_id $user_id > add_reviewer.resp &&
  git clone https://"$user_http_name":"$user_http_password"@"$clone_domain"/"$tenant_id"/"$REPO_NAME".git "$REVIEWER_REPO_DIR" >/dev/null 2>&1 &&
  cd "$REVIEWER_REPO_DIR" &&
  git repo --single download $mr_id &&
  git checkout -b "tmp"`date '+%Y%m%d%H%M%S'` &&
  echo "[test-11][protected]" >> test.file &&
  git add test.file &&
  git commit -m "[test-11][protected]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  git repo upload --single --change $mr_id -o proc-receive-debug=1 > ../test-11 2>&1 &&
  cd "$SHARNESS_TRASH_DIRECTORY"
'

test_expect_success "[test-12][protected] neither reviewer or author,  upload agit flow update CR failure" '
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  mr_id=1 &&
  remove_all_reviewers $REPO_ID $mr_id
  test_expect_code 0

  rm -rf $REVIEWER_REPO_NAME
  git clone https://"$user_http_name":"$user_http_password"@"$clone_domain"/"$tenant_id"/$REPO_NAME.git "$REVIEWER_REPO_NAME" &&
  cd "$REVIEWER_REPO_NAME" &&
  git repo --single download $mr_id &&
  git checkout -b "tmp"`date '+%Y%m%d%H%M%S'` &&
  echo "[test-12][protected]" >> test.file &&
  git add test.file &&
  git commit -m "[test-12][protected]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  test_must_fail git repo upload --single --change $mr_id -o proc-receive-debug=1 > ../test-12 2>&1 &&
  cd "$SHARNESS_TRASH_DIRECTORY" &&
#  grep "The uploader should be CR owner or reviewer" test-12
  grep "failed to push some refs to" test-12
'

test_expect_success "[test-13][non-protected][read] agit flow update CR update success, operator is CR author" '
  branch_id=$(get_protected_branch | jq '.[0].id') &&
  unprotect_branch $branch_id  &&
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  mr_id=1 &&
  cd "$REPO_DIR" &&
  git repo --single download $mr_id &&
  git checkout -b "tmp"`date '+%Y%m%d%H%M%S'` &&
  echo "[test-13][non-protected][read]" >> test.file &&
  git add test.file &&
  git commit -m "[test-13][non-rotected][read]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1
  git repo upload --single --change $mr_id -o proc-receive-debug=1 > ../test-13 2>&1 &&
  cd "$SHARNESS_TRASH_DIRECTORY"
  test_expect_code 0
'

test_expect_success "[test-14][non-protected] agit flow update CR success, operator is CR reviewer" '
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  mr_id=1 &&
  remove_all_reviewers $REPO_ID $mr_id
  add_reviewer $REPO_ID $mr_id $user_id
  test_expect_code 0

  rm -rf $REVIEWER_REPO_NAME
  git clone https://"$user_http_name":"$user_http_password"@"$clone_domain"/"$tenant_id"/"$REPO_NAME".git "$REVIEWER_REPO_DIR" >/dev/null 2>&1 &&
  cd "$REVIEWER_REPO_DIR" &&
  git repo --single download $mr_id &&
  git checkout -b "tmp"`date '+%Y%m%d%H%M%S'` &&
  echo "[test-14][non-protected]" >> test.file &&
  git add test.file &&
  git commit -m "[test-14][non-protected]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  git repo upload --single --change $mr_id -o proc-receive-debug=1 > ../test-14 2>&1 &&
  cd "$SHARNESS_TRASH_DIRECTORY"
  test_expect_code 0
'

test_expect_success "[test-15][non-protected] neither reviewer or author, upload agit flow update CR failure" '
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  mr_id=1 &&
  remove_all_reviewers $REPO_ID $mr_id
  test_expect_code 0

  rm -rf $REVIEWER_REPO_NAME
  git clone https://"$user_http_name":"$user_http_password"@"$clone_domain"/"$tenant_id"/$REPO_NAME.git "$REVIEWER_REPO_NAME" &&
  cd "$REVIEWER_REPO_NAME" &&
  git repo --single download $mr_id &&
  git checkout -b "tmp"`date '+%Y%m%d%H%M%S'` &&
  echo "[test-15][non-protected]" >> test.file &&
  git add test.file &&
  git commit -m "[test-15][non-protected]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  git repo upload --single --change $mr_id -o proc-receive-debug=1 > ../test-15 2>&1
  cd "$SHARNESS_TRASH_DIRECTORY" &&
  grep "failed to push some refs to" test-15
'

test_expect_success "[test-16][non-protected][read-write] Normal push to branch success" '
  add_member $WRITE_LEVEL $another_user_external &&
  git -C  "$REPO_DIR" checkout master &&
  root=$(git -C "$REPO_DIR" log --pretty=format:%H | tail -1) &&
  git -C "$REPO_DIR" reset --hard $root &&
  git -C "$REPO_DIR" push origin HEAD:"$MULTI_BRANCH" > test-16 2>&1
  test_expect_code 0
'

test_expect_success "[test-17][protected][read] agit flow create CR success (target ref with multiple path )" '
  echo "[test-17][non-protected][read]" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "[test-17][protected][read]" --author "dyrone <dyroneteng@gmail.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master:refs/for/"$MULTI_BRANCH" -o proc-receive-debug=1 > test-17 2>&1
'

test_done
